/*
* $Id: veol.js 28 2008-07-06 22:34:42Z nmassart $ 
*/

var oRegExpXML=new RegExp("(<kml xmlns=\"http://earth\.google\.com/kml/2\.0\">.*</kml>)", "g");
var oRegExpStationName=new RegExp("([^,]*)", "g");

var prefs = new _IG_Prefs();
var iUpdateFreq=prefs.getInt("update");
var bOnlyFav=prefs.getBool("onlyfavorits");

var sURL="http://www.veol.caen.fr/localizaciones/localizaciones.php";

function displayMessage(){
	var sMessageID = "2008070602";
	if(prefs.getString("last_closed_message") != sMessageID)
	{
		var oMsgModule = new _IG_MiniMessage(0);
		var oMessage = oMsgModule.createDismissibleMessage(
			"<a href=\"http://code.google.com/p/veol-gadget/wiki/ReleaseNotes\" target=\"_blank\">Version 0.4 beta.</a> <br/><br/>Merci à Nico V pour ses suggestions d'optimisation très éfficaces, voyez comme ça charge vite maintenant !<br/><br/><a href=\"http://code.google.com/p/veol-gadget\" target=\"_blank\">Ce gadget est désormais sur google code.</a>",
			function()
			{
				prefs.set("last_closed_message", sMessageID);
				return true;
			}
		);
	}
}

function getDispoColor(iFirst,iSecond)
{
if(isNaN(iSecond))iSecond=0;
return (!isNaN(iFirst))?((iFirst>0)?((iFirst>iSecond)?"dispoOk":"dispoWarn"):"dispoNok"):"dispoNa";;
}

function isFavorite(iParkNumber)
{
	var aFavPref=prefs.getArray("favoris");
	for(iIndex in aFavPref)
	{
		if(aFavPref[iIndex]==iParkNumber)return true;
	}
	return false;
}

function getParkInfos()
{
	var url = sURL;
	_IG_FetchContent(url, function (responseText)
	{
		var KMLParser=new DOMParser();
		var KMLDoc=KMLParser.parseFromString(responseText.match(oRegExpXML),"text/xml");
		var aPlacemarks=KMLDoc.documentElement.getElementsByTagName('Placemark');
		var HTMLParser=new DOMParser();
		
		var oListContainer = $('#liste-stations');
		
		for (var i=0;i<aPlacemarks.length;i++)
		{
			var idpark = i+1;
			var bIsFavorite = isFavorite(idpark);
			if((!bOnlyFav && !bIsFavorite) || bIsFavorite)
			{
				var HTMLDoc=HTMLParser.parseFromString('<p>'+aPlacemarks[i].textContent+'</p>',"text/xml");
				var aDivs = HTMLDoc.documentElement.getElementsByTagName('div');
				
				var iFreeBikes = parseInt(aDivs[3].childNodes[0].textContent);
				var iFreeParkPlaces = parseInt(aDivs[3].childNodes[2].textContent);
				
				var sInnerHTML='<tr id="row'+idpark+'">';
				sInnerHTML+='<td class="no'+((bIsFavorite)?' favori':'')+'">'+idpark+'</td>';
				sInnerHTML+='<td class="park'+((bIsFavorite)?' favori':'')+'"><a href="http://www.veol.caen.fr/localizaciones/localizaciones.php?p_pfwid_ada='+idpark+'" target="_blank">'+aDivs[1].childNodes[0].textContent.match(oRegExpStationName)[0].replace('\\','')+'</a></td>';
				sInnerHTML+='<td class="'+getDispoColor(iFreeBikes,iFreeParkPlaces)+'">'+((isNaN(iFreeBikes))?"NA":iFreeBikes)+'</td>';
				sInnerHTML+='<td class="'+getDispoColor(iFreeParkPlaces,iFreeBikes)+'">'+((isNaN(iFreeParkPlaces))?"NA":iFreeParkPlaces)+'</td>';
				sInnerHTML+='</tr>';
				oListContainer.append(sInnerHTML);
			}
		}
	},{ refreshInterval: (60 * iUpdateFreq)});
}

displayMessage();
_IG_RegisterOnloadHandler(getParkInfos);